package com.yan.control;

import com.yan.pojo.Student;
import com.yan.pojo.Teacher;
import com.yan.service.AdminService;
import com.yan.service.StudentService;
import com.yan.service.TeacherService;
import com.yan.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

@Controller
public class TeacherController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private StudentService studentService;

    @RequestMapping("/getStudentbyTeacher")
    @ResponseBody
    public Msg getStudentbyTeacher(@RequestParam("teacherId") String teacherId){
        List<Student> list = teacherService.studentByTeacher(Integer.parseInt(teacherId));
        return Msg.success().add("list",list);
    }

    @RequestMapping("/saveEditStudentByTeacher")
    @ResponseBody
    public Msg saveEditStudentByTeacher(@Valid Student student){
        boolean b = teacherService.updateStudent01(student);
        return b ? Msg.success() : Msg.fail();
    }

    @RequestMapping("/isAbleToTeacher")
    @ResponseBody
    public Msg isAbleToTeacher(@RequestParam("teacherId") String teacherId,@RequestParam("studentId") String studentId){
        Integer b = teacherService.isAbleToTeacher(Integer.parseInt(studentId), Integer.parseInt(teacherId));
        if(b == null){
            return Msg.fail();
        }else
        {
            return Msg.success();
        }
    }


    @RequestMapping("/addStuToTeacher")
    @ResponseBody
    public Msg addStuToTeacher(@RequestParam("teacherId") String teacherId,@RequestParam("studentId") String studentId){
        boolean b = teacherService.addStuToTeacher(Integer.parseInt(studentId), Integer.parseInt(teacherId));
        return b ? Msg.success() : Msg.fail();
    }


    @RequestMapping("/removeClass")
    @ResponseBody
    public Msg removeClass(@RequestParam("studentId") String studentId){
        boolean b = teacherService.removeClass(Integer.parseInt(studentId));
        return b ? Msg.success() : Msg.fail();
    }

    @RequestMapping("/removeMoreClass")
    @ResponseBody
    public Msg removeMoreClass(@RequestParam("ids") String ids){
        String[] split = ids.split("-");
        for (int i = 0; i < split.length; i++) {
            teacherService.removeClass(Integer.parseInt(split[i]));
        }
        return Msg.success();
    }

    @RequestMapping("/getTeacherByName02")
    @ResponseBody
    public Msg getTeacherByName(@RequestParam("name") String name){
        Teacher b = teacherService.getTeacherByName(name);
        if(b != null){
            return Msg.success();
        }else{
            return Msg.fail();
        }
    }





}
